//
//  CourseDetailViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/21.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CourseDetailViewController.h"
#import "DetailFourCollectionViewCell.h"
#import "DetailModel.h"
#import "DetailFourSecond.h"
#import "DetailSecondModel.h"
#import "UMSocial.h"
#import "DataBaseManager.h"
#import "UserDetailViewController.h"
@interface CourseDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UMSocialUIDelegate>
@property (nonatomic, retain)UICollectionView *detailView;//collectionView
@property (nonatomic, retain)NSMutableArray *dataArr;//数组
@property (nonatomic, retain)NSDictionary *dic;//自定义Cell的数据
@end

@implementation CourseDetailViewController

-(void)dealloc
{
    [_detailView release];
    [_dataArr release];
    [_dic release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];//背景颜色
    
    UIBarButtonItem *collect = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"collect"] style:UIBarButtonItemStylePlain target:self action:@selector(collect)];
    
    UIBarButtonItem *fenxiang = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(fenxiang)];
    self.navigationItem.rightBarButtonItems = @[collect, fenxiang];
    
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];//导航条返回键带的首页 让他消失
    //数据
    [self data];
    [self detailData];
    //布局
    UICollectionViewFlowLayout *detailLayout = [[UICollectionViewFlowLayout alloc]init];
    //行列结局
    detailLayout.minimumInteritemSpacing = 0;
    detailLayout.minimumLineSpacing = 0;
    //item大小
    
    detailLayout.itemSize =CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    //滑动方向
    detailLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //创建Tableview
    self.detailView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 55, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:detailLayout];
    //代理
    self.detailView.dataSource = self;
    self.detailView.delegate = self;
    //整页滚动
    self.detailView.pagingEnabled = YES;
    //背景颜色
    self.detailView.backgroundColor = [UIColor whiteColor];
    //添加
    [self.view addSubview:self.detailView];
    [_detailView release];
    //重用池第二个
    [_detailView registerClass:[DetailFourCollectionViewCell class] forCellWithReuseIdentifier:@"detailcell"];
    //第一个重用池
    [_detailView registerClass:[DetailFourSecond class] forCellWithReuseIdentifier:@"cell"];
    [detailLayout release];
}
#pragma mark - CollectionView必须实现的方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //如果item == 0 的时候条其他cell
    if (indexPath.item == 0) {
        DetailFourSecond *desec = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        desec.titlelabel.text = _dic[@"subject"];//标题
        [desec.bgimg sd_setImageWithURL:[NSURL URLWithString:_dic[@"host_pic_s"]]];//图片
        desec.comtentlabel.text = _dic[@"summary"];//内容
        [desec.touxiang sd_setImageWithURL:[NSURL URLWithString:_dic[@"face_pic"]]];//头像
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action)];//手势
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [desec.touxiang addGestureRecognizer:tap];
        [tap release];
        desec.name.text = _dic[@"user_name"];//作者
        desec.other.text = [[[[[[[_dic[@"view_o"]stringByAppendingString:@"人气 | "] stringByAppendingString:_dic[@"collect"]]stringByAppendingString:@"收藏 | "] stringByAppendingString:_dic[@"comment_num"]] stringByAppendingString:@"评论 | "]stringByAppendingString:_dic[@"laud"]]stringByAppendingString:@"赞"];//其他赞, 评论. 收藏
        return desec;
    }else
    {
    DetailFourCollectionViewCell *detailcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"detailcell" forIndexPath:indexPath];
    DetailModel *detail = _dataArr[indexPath.item];
    [detailcell.img sd_setImageWithURL:[NSURL URLWithString:detail.pic]];//图片
    detailcell.content.text = detail.content;//内容

    return detailcell;
    }
}
#pragma mark - 数据处理
- (void)data
{
    
    NSString *str =[[@"http://m.shougongke.com/index.php?&c=Course&a=CourseDetial&id="stringByAppendingString:self.deid]stringByAppendingString:@"&vid=15"];
    [AFNnetWork GETwithURL:str completion:^(id result) {
        
        NSArray *arr = result[@"data"][@"step"];
        self.dataArr = [DetailModel modelHandlerWithArray:arr];
        [_detailView reloadData];
    }];
    
}

#pragma mark - 自定义cell第一个的数据
- (void)detailData
{
    NSString *str = [[@"http://m.shougongke.com/index.php?&c=Course&a=CourseDetial&id=" stringByAppendingString:self.deid] stringByAppendingString:@"&vid=15"];
    [AFNnetWork GETwithURL:str completion:^(id result) {
        self.dic = result[@"data"];
        NSLog(@"%@",_dic[@"user_id"]);
        [_detailView reloadData];
    }];
}
- (void)fenxiang
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56a87643e0f55a9fc20020c2"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,nil]
                                       delegate:self];
}
- (void)collect
{
   
    
    DataBaseManager *dbManger = [DataBaseManager shareInstance];
    
    [dbManger openDB];
    
    [[DataBaseManager shareInstance] createTable];
    
    
    
    DetailSecondModel *m = [[DetailSecondModel alloc]init];
    m.subject = self.mo.subject;
    NSMutableArray *arr1 = [[DataBaseManager shareInstance] selectMovie];
    
    for (DetailSecondModel *sm in arr1) {
        if ([sm.subject isEqualToString: m.subject]) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertController animated:YES completion:nil];
            [self performSelector:@selector(diss:) withObject:@"ssss" afterDelay:2];
            return;
            
        }
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:nil];
    [self performSelector:@selector(diss:) withObject:@"ssss" afterDelay:2];
    
    [[DataBaseManager shareInstance]insertMovie:m];
    
}

- (void)diss:(id)a
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)action
{
    UserDetailViewController *user = [[UserDetailViewController alloc]init];
    
    user.uid = [_dic objectForKey:@"user_id"];
    user.faceurl = [_dic objectForKey:@"face_pic"];
    user.namelabel = [_dic objectForKey:@"user_name"];
    [self.navigationController pushViewController:user animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
